import { Dimensions, PixelRatio, Platform, StatusBar } from 'react-native';

// 屏幕尺寸
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

// 设计稿基准尺寸（以iPhone 6/7/8为基准: 375 x 667）
const baseWidth = 375;
const baseHeight = 667;

// 缩放比例
const scaleWidth = SCREEN_WIDTH / baseWidth;
const scaleHeight = SCREEN_HEIGHT / baseHeight;

// 根据宽度缩放
export const scale = (size: number) => Math.round(scaleWidth * size);

// 根据高度缩放
export const verticalScale = (size: number) => Math.round(scaleHeight * size);

// 适度缩放（较小的尺寸使用较小的缩放系数）
export const moderateScale = (size: number, factor = 0.5) => 
  Math.round(size + (scale(size) - size) * factor);

// 字体缩放（考虑系统字体大小设置）
export const fontScale = (size: number) => {
  const newSize = scale(size);
  if (Platform.OS === 'ios') {
    return Math.round(PixelRatio.roundToNearestPixel(newSize));
  } else {
    return Math.round(PixelRatio.roundToNearestPixel(newSize)) - 2;
  }
};

// 获取状态栏高度
export const getStatusBarHeight = () => {
  if (Platform.OS === 'android') {
    return StatusBar.currentHeight || 0;
  }
  return Platform.OS === 'ios' ? (SCREEN_HEIGHT >= 812 ? 44 : 20) : 0;
};

// 获取屏幕底部安全区域的高度（针对iPhone X及以上机型）
export const getBottomSpace = () => {
  if (Platform.OS === 'ios' && SCREEN_HEIGHT >= 812) {
    return 34; // iPhone X及以上机型
  }
  return 0;
};

// 常用尺寸
export const Sizes = {
  // 字体尺寸
  fontTiny: fontScale(10),
  fontSmall: fontScale(12),
  fontRegular: fontScale(14),
  fontMedium: fontScale(16),
  fontLarge: fontScale(18),
  fontXL: fontScale(20),
  fontXXL: fontScale(24),
  
  // 间距
  spacingTiny: scale(4),
  spacingSmall: scale(8),
  spacingRegular: scale(12),
  spacingMedium: scale(16),
  spacingLarge: scale(20),
  spacingXL: scale(24),
  spacingXXL: scale(32),
  
  // 圆角
  borderRadiusSmall: scale(4),
  borderRadiusRegular: scale(8),
  borderRadiusMedium: scale(12),
  borderRadiusLarge: scale(16),
  
  // 按钮高度
  buttonHeightSmall: verticalScale(32),
  buttonHeightRegular: verticalScale(40),
  buttonHeightLarge: verticalScale(48),
};

// 屏幕尺寸
export const Screen = {
  width: SCREEN_WIDTH,
  height: SCREEN_HEIGHT,
  statusBarHeight: getStatusBarHeight(),
  bottomSpaceHeight: getBottomSpace(),
  isSmallDevice: SCREEN_WIDTH < 360,
  isMediumDevice: SCREEN_WIDTH >= 360 && SCREEN_WIDTH < 400,
  isLargeDevice: SCREEN_WIDTH >= 400,
  isIphoneX: Platform.OS === 'ios' && SCREEN_HEIGHT >= 812,
}; 